<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_addressMatchService"></title>
    <style>
        .input-group {
            margin: 8px 0;
        }

        .control {
            position: absolute;
            top: 10px;
            right: 10px;
        }

        .ol-popup {
            position: absolute;
            background-color: white;
            -webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));
            padding: 15px;
            border-radius: 10px;
            border: 1px solid #cccccc;
            bottom: 12px;
            left: -50px;
            min-width: 300px;
        }

        .ol-popup:after, .ol-popup:before {
            top: 100%;
            border: solid transparent;
            content: " ";
            height: 0;
            width: 0;
            position: absolute;
            pointer-events: none;
        }

        .ol-popup:after {
            border-top-color: white;
            border-width: 10px;
            left: 48px;
            margin-left: -10px;
        }

        .ol-popup:before {
            border-top-color: #cccccc;
            border-width: 11px;
            left: 48px;
            margin-left: -11px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%; position: absolute;top: 0;">
<div id="map" style="width: 100%;height:100%"></div>
<div id="control" class="control" style='width: 350px'>
    <div class="panel panel-default">
        <div class="panel-body">
            <ul class="nav nav-tabs nav-justified">
                <li id="geocode" role="presentation" class="active"><a href="#" data-i18n="resources.text_code"></a>
                </li>
                <li id="geodecode" role="presentation"><a href="#" data-i18n="resources.text_decode"></a></li>
            </ul>

            <div class="tab-content">
                <div id="geocodeParam" role="tabpanel" class="tab-pane deplistContent active">
                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_address"></span>
                        <input id="address" type="text" class="form-control" placeholder="超图软件"/>
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_filterField"></span>
                        <input id="filters" type="text" class="form-control"
                               placeholder="北京市,朝阳区"/>
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_minIndex"></span>
                        <input id="fromIndex" type="text" class="form-control"
                               placeholder="0"/>
                    </div>
                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_maxIndex"></span>
                        <input id="toIndex" type="text" class="form-control"
                               placeholder="10"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_maxCount"></span>
                        <input id="maxReturn" type="text" class="form-control"
                               placeholder="-1"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_coorSystem"></span>
                        <input id="prjCoordSys" type="text" class="form-control"
                               placeholder="{epsgcode:4326}"/>
                    </div>

                    <div align="right">
                        <input type="button" id='codeBtn' class="btn btn-primary"
                               data-i18n="[value]resources.text_input_value_match"/>
                    </div>
                </div>
                <div id="geodecodeParam" role="tabpanel" class="tab-pane deplistContent">
                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_abscissa"></span>
                        <input id="xCoord" type="text" class="form-control" placeholder="116.3518541194752"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_ordinate"></span>
                        <input id="yCoord" type="text" class="form-control"
                               placeholder="40.00097839595237"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_filterField"></span>
                        <input id="filters2" type="text" class="form-control"
                               placeholder=""/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_minIndex"></span>
                        <input id="fromIndex2" type="text" class="form-control"
                               placeholder="0"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_maxIndex"></span>
                        <input id="toIndex2" type="text" class="form-control"
                               placeholder="10"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_maxCount"></span>
                        <input id="maxReturn2" type="text" class="form-control"
                               placeholder="-1"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_queryRadius"></span>
                        <input id="geoDecodingRadius" type="text" class="form-control"
                               placeholder="-1"/>
                    </div>

                    <div class="input-group">
                        <span class="input-group-addon" data-i18n="resources.text_coorSystem"></span>
                        <input id="prjCoordSys2" type="text" class="form-control"
                               placeholder="{epsgcode:4326}"/>
                    </div>

                    <div align="right">
                        <input type="button" id='decodeBtn' class="btn btn-primary"
                               data-i18n="[value]resources.text_input_value_match"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="info" class="ol-popup">
    <div id="info-content"></div>
</div>
<script type="text/javascript" include="jquery,bootstrap-css" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
<script type="text/javascript">
    var map,select,
        url = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/map-china/rest/maps/China_4326",
        addressUrl = (window.isLocal ? window.server : "https://iserver.supermap.io") + "/iserver/services/addressmatch-Address/restjsr/v1/address",
        imgUrl = "../img/markerbig_select.png",
        vectorSource = new ol.source.Vector(),
        addressMatchService = new ol.supermap.AddressMatchService(addressUrl),
        view = new ol.View({
            center: [116.383572, 39.914714],
            zoom: 12,
            projection: 'EPSG:4326',
            multiWorld: true
        }),
        container = document.getElementById('info'),
        content = document.getElementById('info-content'),
        overlay = new ol.Overlay(({
            element: container,
            autoPan: true,
            autoPanAnimation: {
                duration: 250
            },
            offset: [0, -20]
        }));
    map = new ol.Map({
        target: 'map',
        // ol v7版本用法为ol.control.defaults.defaults； v6版本以下用法为ol.control.defaults
        controls: ol.control.defaults.defaults({attributionOptions: {collapsed: false}})
            .extend([new ol.supermap.control.Logo({ link: "https://iclient.supermap.io" })]),
        view: view,
        overlays: [overlay]
    });
    var control = new ol.control.Control({element: document.getElementById('control')});
    control.setMap(map);
    map.addControl(control);

    var layer = new ol.layer.Tile({
        source: new ol.source.TileSuperMapRest({
            url: url,
            wrapX: true,
            projection: 'EPSG:4326',
            origin: [-180.0, 90]
        })
    });
    var vectorLayer = new ol.layer.Vector({
        source: vectorSource
    });
    map.addLayer(layer);
    map.addLayer(vectorLayer);

    $("#geocode").click(function () {
        $("#geocode")[0].className = 'active';
        $("#geodecode")[0].className = '';
        $("#geocodeParam").addClass('active');
        $("#geodecodeParam").removeClass('active');
    });
    $("#geodecode").click(function () {
        $("#geocode")[0].className = '';
        $("#geodecode")[0].className = 'active';
        $("#geodecodeParam").addClass('active');
        $("#geocodeParam").removeClass('active');
    });

    //判断输入字符串是否为空或者全部都是空格
    function isNull(str) {
        if (str == "") return true;
        var regu = "^[ ]+$";
        var re = new RegExp(regu);
        return re.test(str);
    }

    $("#codeBtn").click(function () {
        var geoCodeParam = new ol.supermap.GeoCodingParameter({
            address: $('#address').val() || $('#address').attr('placeholder'),
            fromIndex: $('#fromIndex').val() || $('#fromIndex').attr('placeholder'),
            toIndex: $('#toIndex').val() || $('#toIndex').attr('placeholder'),
            filters: $('#filters').val() || $('#filters').attr('placeholder'),
            prjCoordSys: $('#prjCoordSys').val() || $('#prjCoordSys').attr('placeholder'),
            maxReturn: $('#maxReturn').val() || $('#maxReturn').attr('placeholder')
        });
        // addressMatchService.code(geoCodeParam, match);
        addressMatchService.code(geoCodeParam).then(match);
    });
    $("#decodeBtn").click(function () {
        var geoDecodeParam = new ol.supermap.GeoDecodingParameter({
            x: $('#xCoord').val() || $('#xCoord').attr('placeholder'),
            y: $('#yCoord').val() || $('#yCoord').attr('placeholder'),
            fromIndex: $('#fromIndex2').val() || $('#fromIndex2').attr('placeholder'),
            toIndex: $('#toIndex2').val() || $('#toIndex2').attr('placeholder'),
            filters: $('#filters2').val() || $('#filters2').attr('placeholder'),
            prjCoordSys: $('#prjCoordSys2').val() || $('#prjCoordSys2').attr('placeholder'),
            maxReturn: $('#maxReturn2').val() || $('#maxReturn2').attr('placeholder'),
            geoDecodingRadius: $('#geoDecodingRadius').val() || $('#geoDecodingRadius').attr('placeholder'),
        });
        // addressMatchService.decode(geoDecodeParam, match);
        addressMatchService.decode(geoDecodeParam).then(match);
    });

    function match(obj) {
        select && map.removeInteraction(select);
        overlay.setPosition(undefined);
        vectorSource.clear();
        var features = [];

        obj.result.map(function (item) {
            var feature = new ol.Feature();
            feature.setGeometry(new ol.geom.Point([item.location.x, item.location.y]));
            feature.setProperties({
                location: item.location,
                address: item.address,
                score: item.score,
                filters: item.filters
            });
            feature.setStyle(new ol.style.Style({
                image: new ol.style.Icon(({
                    anchor: [0.5, 1],
                    src: imgUrl
                }))
            }));
            features.push(feature);
        });
        select = new ol.interaction.Select({
            style: new ol.style.Style({
                image: new ol.style.Icon(({
                    anchor: [0.5, 1],
                    src: imgUrl
                }))
            })
        });
        select.on('select', function (e) {
            if (e.selected.length > 0) {
                var feature = e.selected[0];
                var innerHTML = "";
                innerHTML += resources.text_companyAddress + feature.getProperties().address + "<br>";
                var x = Number(feature.getProperties().location.x.toString().match(/^\d+(?:\.\d{0,2})?/));
                var y = Number(feature.getProperties().location.y.toString().match(/^\d+(?:\.\d{0,2})?/));
                innerHTML += resources.text_coordinate+":[" + x + "," + y + "]<br>";
                if (feature.getProperties().score > 0) {
                    innerHTML += resources.text_matchDegree+":" + feature.getProperties().score + "<br>";
                }
                innerHTML += resources.text_filterField+":" + feature.getProperties().filters + "<br>";
                content.innerHTML = innerHTML;
                overlay.setPosition(feature.getGeometry().getCoordinates());
            } else if (overlay) {
                overlay.setPosition(undefined);
            }
        });
        map.addInteraction(select);
        vectorSource.addFeatures(features);
        view.animate({zoom: 11}, {center: [116.383572, 39.914714]});
    }
</script>
</body>
</html>